package lzyc.pointsManagement.business.company.service.impl;

import lzyc.pointsManagement.business.company.domain.CompanyInfo;
import lzyc.pointsManagement.business.company.mapper.CompanyInfoMapper;
import lzyc.pointsManagement.business.company.service.CompanyInfoService;
import lzyc.pointsManagement.business.config.DeptGroupEnum;
import lzyc.pointsManagement.common.core.domain.entity.SysDept;
import lzyc.pointsManagement.common.core.domain.entity.SysUser;
import lzyc.pointsManagement.system.mapper.SysDeptMapper;
import lzyc.pointsManagement.system.mapper.SysUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * (CompanyInfo)表服务实现类
 *
 * @author yangyang
 * @since 2024-03-10 17:09:15
 */
@Service("companyInfoService")
public class CompanyInfoServiceImpl implements CompanyInfoService {
    @Resource
    private CompanyInfoMapper companyInfoMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private SysUserMapper sysUserMapper;
    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<CompanyInfo> queryAllByLimit(int offset, int limit) {
        return this.companyInfoMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 修改数据
     *
     * @param companyInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int update(CompanyInfo companyInfo) {
        return this.companyInfoMapper.update(companyInfo);
    }

    /**
     * 获取支部信息
     * 
     * @return
     */
    @Override
    public Optional<Object> getCompanyInfo() {
        List<CompanyInfo> list = queryAllByLimit(0, 1);
        if (list.isEmpty()) {
            return Optional.empty();
        }
        CompanyInfo companyInfo = list.get(0);
        companyInfo.setUserNum(Integer.toString(getUserNum()));
        companyInfo.setGroupNum(Integer.toString(getGroupNum()));
        return Optional.ofNullable(companyInfo);
    }

    /**
     * 获取党支部小组数
     * 
     * @return
     */
    private int getGroupNum() {
        SysDept sysDept=new SysDept();
        sysDept.setLeader(DeptGroupEnum.GROUP_TYPE.getValue());
        return sysDeptMapper.selectDeptCount(sysDept);
    }

    /**
     * 获取党员数
     * 
     * @return
     */
    private int getUserNum() {
        return sysUserMapper.selectUserCount();
    }
}